import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/button/radius_button.dart';
import 'package:boss/ui/widget/modal/modal.dart';
import 'package:boss/util/widget_to_local.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:hi_common/hi_common.dart';
import 'package:qr_flutter/qr_flutter.dart';

Future<void> showPayQrCodeModal(BuildContext context, String qrCode) {
  return showModal(context, builder: (context) => _PayQrCodeModal(qrCode));
}

class _PayQrCodeModal extends StatefulWidget {
  final String qrCode;

  const _PayQrCodeModal(this.qrCode);

  @override
  __PayQrCodeModalState createState() => __PayQrCodeModalState();
}

class __PayQrCodeModalState extends State<_PayQrCodeModal> {
  GlobalKey globalKey = GlobalKey();

  String qrCode;

  @override
  void initState() {
    super.initState();

    final uri = Uri.parse(widget.qrCode);

    qrCode = uri.queryParameters['qrcode'];
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 600.w,
      padding: EdgeInsets.symmetric(vertical: 40.w),
      child: Column(
        children: [
          RepaintBoundary(
            key: globalKey,
            child: Container(
              padding: EdgeInsets.all(2),
              color: Colors.white,
              child: Container(
                width: 380.w,
                height: 500.w,
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage('assets/image/pay_qr_code_bg.png'),
                  ),
                ),
                child: Container(
                  margin: EdgeInsets.only(top: 99.w),
                  child: Column(
                    children: [
                      Container(
                        width: 280.w,
                        height: 280.w,
                        padding: EdgeInsets.all(10.w),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(10),
                        ),
                        alignment: Alignment.center,
                        child: QrImage(
                          size: 245.w,
                          data: widget.qrCode,
                          padding: EdgeInsets.zero,
                        ),
                      ),
                      Container(
                        child: Text(qrCode, style: TextStyleConstant().white_24),
                        margin: EdgeInsets.only(top: 8.w),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
          RadiusButton(
            width: 500.w,
            height: 90.w,
            margin: EdgeInsets.only(top: 20.w),
            title: '保存到手机',
            onTap: widget2local,
          ),
        ],
      ),
    );
  }

  widget2local() async {
    Widget2Local.save(globalKey);
  }
}
